package com.example.project.projectspringcloud.controller.admin.order;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.common.OperationException;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.RespBody;
import com.example.project.projectspringcloud.entity.order.OrderCombo;
import com.example.project.projectspringcloud.entity.sys.Area;
import com.example.project.projectspringcloud.service.order.OrderComboService;
import com.example.project.projectspringcloud.service.sys.SysAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "管理员套餐配置")
@Controller
@RequestMapping(value = "/admin/combo")
public class AdminOrderComboController {

    @Autowired
    private OrderComboService orderComboService;

    @Autowired
    private SysAreaService sysAreaService;

    @ApiOperation(value = "套餐列表", notes = "返回套餐列表")
    @RequestMapping(value = {"", "/list"})
    public String list(OrderCombo orderCombo, @RequestParam(value = "type", required = false) Integer type,
                       Page page, Model model) {
        List<OrderCombo> orderComboList = orderComboService.columnList(orderCombo, type);
        model.addAttribute("orderComboList", orderComboList);
        model.addAttribute("orderCombo", orderCombo);
        model.addAttribute("type", type);
        return "admin/order/order-combo-list";
    }

    @ApiOperation(value = "套餐表单", notes = "返回套餐表单")
    @GetMapping(value = "/form")
    public String form(OrderCombo orderCombo, @RequestParam(value = "type", required = false) String type, Model model) {
        if (orderCombo.getId() != null) {
            model.addAttribute("orderCombo", orderComboService.get(orderCombo));
        }

        Area entity = new Area();
        entity.setIsOpen(1);
        List<Area> areas = sysAreaService.list(entity, null);
        model.addAttribute("areas", areas);

        model.addAttribute("type", type);
        return "admin/order/order-combo-form";
    }

    @ApiOperation(value = "API保存", notes = "保存")
    @PostMapping(value = "/api/save")
    @ResponseBody
    public RespBody save(OrderCombo orderCombo) {
        boolean result = orderComboService.save(orderCombo);
        if (!result) {
            return RespBody.error(Constants.Message.SAVE_FAILED);
        }

        return RespBody.ok(Constants.Message.SAVE_SUCCESS);
    }
}
